1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package javax.security.auth;
27
28 import java.util.*;
29 import java.text.MessageFormat;
30 import java.security.Permission;
31 import java.security.PermissionCollection;
32 import java.security.Principal;
33 import sun.security.util.ResourcesMgr;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public final class PrivateCredentialPermission extends Permission {
105
106 private static final long serialVersionUID = 5284372143517237068L;
107
108 private static final CredOwner[] EMPTY_PRINCIPALS = new CredOwner[0];
109
110
111
112
113 private String credentialClass;
114
115
116
117
118
119
120 private Set principals;
121 private transient CredOwner[] credOwners;
122
123
124
125
126 private boolean testing = false;
127
128
129
130
131
132 PrivateCredentialPermission(String credentialClass,
133 Set<Principal> principals) {
134
135 super(credentialClass);
136 this.credentialClass = credentialClass;
137
138 synchronized(principals) {
139 if (principals.size() == 0) {
140 this.credOwners = EMPTY_PRINCIPALS;
141 } else {
142 this.credOwners = new CredOwner[principals.size()];
143 int index = 0;
144 Iterator<Principal> i = principals.iterator();
145 while (i.hasNext()) {
146 Principal p = i.next();
147 this.credOwners[index++] = new CredOwner
148 (p.getClass().getName(),
149 p.getName());
150 }
151 }
152 }
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public PrivateCredentialPermission(String name, String actions) {
171 super(name);
172
173 if (!"read".equalsIgnoreCase(actions))
174 throw new IllegalArgumentException
175 (ResourcesMgr.getString("actions.can.only.be.read."));
176 init(name);
177 }
178
179
180
181
182
183
184
185
186
187
188 public String getCredentialClass() {
189 return credentialClass;
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 public String[][] getPrincipals() {
211
212 if (credOwners == null || credOwners.length == 0) {
213 return new String[0][0];
214 }
215
216 String[][] pArray = new String[credOwners.length][2];
217 for (int i = 0; i < credOwners.length; i++) {
218 pArray[i][0] = credOwners[i].principalClass;
219 pArray[i][1] = credOwners[i].principalName;
220 }
221 return pArray;
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249 public boolean implies(Permission p) {
250
251 if (p == null || !(p instanceof PrivateCredentialPermission))
252 return false;
253
254 PrivateCredentialPermission that = (PrivateCredentialPermission)p;
255
256 if (!impliesCredentialClass(credentialClass, that.credentialClass))
257 return false;
258
259 return impliesPrincipalSet(credOwners, that.credOwners);
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279 public boolean equals(Object obj) {
280 if (obj == this)
281 return true;
282
283 if (! (obj instanceof PrivateCredentialPermission))
284 return false;
285
286 PrivateCredentialPermission that = (PrivateCredentialPermission)obj;
287
288 return (this.implies(that) && that.implies(this));
289 }
290
291
292
293
294
295
296 public int hashCode() {
297 return this.credentialClass.hashCode();
298 }
299
300
301
302
303
304
305
306
307
308 public String getActions() {
309 return "read";
310 }
311
312
313
314
315
316
317
318
319
320
321
322 public PermissionCollection newPermissionCollection() {
323 return null;
324 }
325
326 private void init(String name) {
327
328 if (name == null || name.trim().length() == 0) {
329 throw new IllegalArgumentException("invalid empty name");
330 }
331
332 ArrayList<CredOwner> pList = new ArrayList<>();
333 StringTokenizer tokenizer = new StringTokenizer(name, " ", true);
334 String principalClass = null;
335 String principalName = null;
336
337 if (testing)
338 System.out.println("whole name = " + name);
339
340
341 credentialClass = tokenizer.nextToken();
342 if (testing)
343 System.out.println("Credential Class = " + credentialClass);
344
345 if (tokenizer.hasMoreTokens() == false) {
346 MessageFormat form = new MessageFormat(ResourcesMgr.getString
347 ("permission.name.name.syntax.invalid."));
348 Object[] source = {name};
349 throw new IllegalArgumentException
350 (form.format(source) + ResourcesMgr.getString
351 ("Credential.Class.not.followed.by.a.Principal.Class.and.Name"));
352 }
353
354 while (tokenizer.hasMoreTokens()) {
355
356
357 tokenizer.nextToken();
358
359
360 principalClass = tokenizer.nextToken();
361 if (testing)
362 System.out.println(" Principal Class = " + principalClass);
363
364 if (tokenizer.hasMoreTokens() == false) {
365 MessageFormat form = new MessageFormat(ResourcesMgr.getString
366 ("permission.name.name.syntax.invalid."));
367 Object[] source = {name};
368 throw new IllegalArgumentException
369 (form.format(source) + ResourcesMgr.getString
370 ("Principal.Class.not.followed.by.a.Principal.Name"));
371 }
372
373
374 tokenizer.nextToken();
375
376
377 principalName = tokenizer.nextToken();
378
379 if (!principalName.startsWith("\"")) {
380 MessageFormat form = new MessageFormat(ResourcesMgr.getString
381 ("permission.name.name.syntax.invalid."));
382 Object[] source = {name};
383 throw new IllegalArgumentException
384 (form.format(source) + ResourcesMgr.getString
385 ("Principal.Name.must.be.surrounded.by.quotes"));
386 }
387
388 if (!principalName.endsWith("\"")) {
389
390
391
392
393
394 while (tokenizer.hasMoreTokens()) {
395 principalName = principalName + tokenizer.nextToken();
396 if (principalName.endsWith("\""))
397 break;
398 }
399
400 if (!principalName.endsWith("\"")) {
401 MessageFormat form = new MessageFormat
402 (ResourcesMgr.getString
403 ("permission.name.name.syntax.invalid."));
404 Object[] source = {name};
405 throw new IllegalArgumentException
406 (form.format(source) + ResourcesMgr.getString
407 ("Principal.Name.missing.end.quote"));
408 }
409 }
410
411 if (testing)
412 System.out.println("\tprincipalName = '" + principalName + "'");
413
414 principalName = principalName.substring
415 (1, principalName.length() - 1);
416
417 if (principalClass.equals("*") &&
418 !principalName.equals("*")) {
419 throw new IllegalArgumentException(ResourcesMgr.getString
420 ("PrivateCredentialPermission.Principal.Class.can.not.be.a.wildcard.value.if.Principal.Name.is.not.a.wildcard.value"));
421 }
422
423 if (testing)
424 System.out.println("\tprincipalName = '" + principalName + "'");
425
426 pList.add(new CredOwner(principalClass, principalName));
427 }
428
429 this.credOwners = new CredOwner[pList.size()];
430 pList.toArray(this.credOwners);
431 }
432
433 private boolean impliesCredentialClass(String thisC, String thatC) {
434
435
436 if (thisC == null || thatC == null)
437 return false;
438
439 if (testing)
440 System.out.println("credential class comparison: " +
441 thisC + "/" + thatC);
442
443 if (thisC.equals("*"))
444 return true;
445
446
447
448
449
450
451
452
453
454
455
456
457 return thisC.equals(thatC);
458 }
459
460 private boolean impliesPrincipalSet(CredOwner[] thisP, CredOwner[] thatP) {
461
462
463 if (thisP == null || thatP == null)
464 return false;
465
466 if (thatP.length == 0)
467 return true;
468
469 if (thisP.length == 0)
470 return false;
471
472 for (int i = 0; i < thisP.length; i++) {
473 boolean foundMatch = false;
474 for (int j = 0; j < thatP.length; j++) {
475 if (thisP[i].implies(thatP[j])) {
476 foundMatch = true;
477 break;
478 }
479 }
480 if (!foundMatch) {
481 return false;
482 }
483 }
484 return true;
485 }
486
487
488
489
490 private void readObject(java.io.ObjectInputStream s) throws
491 java.io.IOException,
492 ClassNotFoundException {
493
494 s.defaultReadObject();
495
496
497
498 if (getName().indexOf(" ") == -1 && getName().indexOf("\"") == -1) {
499
500
501 credentialClass = getName();
502 credOwners = EMPTY_PRINCIPALS;
503
504 } else {
505
506
507 init(getName());
508 }
509 }
510
511
512
513
514 static class CredOwner implements java.io.Serializable {
515
516 private static final long serialVersionUID = -5607449830436408266L;
517
518
519
520
521 String principalClass;
522
523
524
525 String principalName;
526
527 CredOwner(String principalClass, String principalName) {
528 this.principalClass = principalClass;
529 this.principalName = principalName;
530 }
531
532 public boolean implies(Object obj) {
533 if (obj == null || !(obj instanceof CredOwner))
534 return false;
535
536 CredOwner that = (CredOwner)obj;
537
538 if (principalClass.equals("*") ||
539 principalClass.equals(that.principalClass)) {
540
541 if (principalName.equals("*") ||
542 principalName.equals(that.principalName)) {
543 return true;
544 }
545 }
546
547
548
549
550
551 return false;
552 }
553
554 public String toString() {
555 MessageFormat form = new MessageFormat(ResourcesMgr.getString
556 ("CredOwner.Principal.Class.class.Principal.Name.name"));
557 Object[] source = {principalClass, principalName};
558 return (form.format(source));
559 }
560 }
561 }